package com.xinghou.platform.dao.main.aspect;

import com.xinghou.platform.dao.main.StoreDAO;
import com.xinghou.platform.model.main.StoreStatistics;
import org.aspectj.lang.ProceedingJoinPoint;

import com.xinghou.platform.dao.main.common.DBStoreLevelCached;
import com.xinghou.platform.model.main.Store;
import com.xinghou.platform.util.JsonUtil;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Aspect
@Component
public class StoreCacheAspect extends DBStoreLevelCached {

	@Resource
	StoreDAO storeDAO;
	@Pointcut("execution(public * com.xinghou.platform.dao.main.StoreDAO.getStoreBystoreId(..))")
	public void getStoreBystoreId(){
	}

	@Pointcut("execution(public * com.xinghou.platform.dao.main.StoreDAO.updateStore(..))")
	public void updateStore(){
	}

	@Pointcut("execution(public * com.xinghou.platform.dao.main.StoreDAO.delStore(..))")
	public void delStore(){
	}

	@Pointcut("execution(public * com.xinghou.platform.dao.main.StoreDAO.getStoreStatistics(..))")
	public void getStoreStatistics(){
	}
	@Pointcut("execution(public * com.xinghou.platform.dao.main.StoreDAO.addShareCountStatstics(..))")
	public void addShareCountStatstics(){
	}
	@Pointcut("execution(public * com.xinghou.platform.dao.main.StoreDAO.reduceShareCountStatstics(..))")
	public void reduceShareCountStatstics(){
	}




	@Around("addShareCountStatstics()")
	public void addShareCountStatstics(ProceedingJoinPoint pjp) throws Throwable{
		String id = ((Object[])pjp.getArgs())[0].toString();
		pjp.proceed();
		String key = String.format(CACHE_STORE_UID , id);
		Object jsonToken = getRedisClient().get(key, CACHED_STORE_DB_INDEX);
		if(jsonToken!=null) {
			Store store = (Store) JsonUtil.fromJson(jsonToken.toString(), Store.class);
			store.setSharecount(store.getSharecount()+1);
			getRedisClient().set(key, JsonUtil.toJson(store), CACHED_STORE_DB_INDEX);
		}

	}
	@Around("reduceShareCountStatstics()")
	public void  reduceShareCountStatstics(ProceedingJoinPoint pjp) throws Throwable{
		String id = ((Object[])pjp.getArgs())[0].toString();
		pjp.proceed();
		String key = String.format(CACHE_STORE_UID, id);
		Object jsonToken = getRedisClient().get(key, CACHED_STORE_DB_INDEX);
		if(jsonToken!=null) {
			Store store = (Store) JsonUtil.fromJson(jsonToken.toString(), Store.class);
			store.setSharecount(store.getSharecount()-1);
			getRedisClient().set(key, JsonUtil.toJson(store), CACHED_STORE_DB_INDEX);
		}
	}
	@Around("getStoreStatistics()")
	public StoreStatistics getStoreStatistics(ProceedingJoinPoint pjp) throws Throwable{
		String id = ((Object[])pjp.getArgs())[0].toString();
		String key = String.format(CACHE_STORE_STATSTICS , id);
		Object jsonToken = getRedisClient().get(key, CACHED_STORE_DB_INDEX);
		if(jsonToken == null){
			StoreStatistics storeStatistics = (StoreStatistics)pjp.proceed();
			if(storeStatistics != null)
				getRedisClient().set(key, JsonUtil.toJson(storeStatistics), CACHED_STORE_DB_INDEX);
			return storeStatistics;
		}else{
			StoreStatistics storeStatistics = (StoreStatistics)JsonUtil.fromJson(jsonToken.toString(), StoreStatistics.class);
			return storeStatistics;
		}
	}







	@Around("getStoreBystoreId()")
	public Store getStoreBystoreId(ProceedingJoinPoint pjp) throws Throwable{
		String id = ((Object[])pjp.getArgs())[0].toString();
		String key = String.format(CACHE_STORE_UID , id);
		Object jsonToken = getRedisClient().get(key, CACHED_STORE_DB_INDEX);
		if(jsonToken == null){
			Store store = (Store)pjp.proceed();
			if(store != null)
				getRedisClient().set(key, JsonUtil.toJson(store), CACHED_STORE_DB_INDEX);
			return store;
		}else{
			Store store = (Store)JsonUtil.fromJson(jsonToken.toString(), Store.class);
			return store;
		}
	}

	@Around("updateStore()")
	public int updateStore(ProceedingJoinPoint pjp) throws Throwable{
		int returnno = (int) pjp.proceed();
		Store store = (Store)((Object[])pjp.getArgs())[0];
		String keyInfoId = String.format(CACHE_STORE_UID , store.getId().toString());
		getRedisClient().del(keyInfoId, CACHED_STORE_DB_INDEX);
		storeDAO.getStoreBystoreId(store.getId());
		/*
		if(getRedisClient().exists(keyInfoId, CACHED_STORE_DB_INDEX)){
			getRedisClient().set(keyInfoId, JsonUtil.toJson(store), CACHED_STORE_DB_INDEX);
		}
		*/
		return returnno;
	}

	@Around("delStore()")
	public int delStore(ProceedingJoinPoint pjp) throws Throwable{
		int returnno = (int) pjp.proceed();
		Store store = (Store)((Object[])pjp.getArgs())[0];
		String keyInfoId = String.format(CACHE_STORE_UID , store.getId().toString());
		getRedisClient().del(keyInfoId, CACHED_STORE_DB_INDEX);
		/*
		if(getRedisClient().exists(keyInfoId, CACHED_STORE_DB_INDEX)){
			getRedisClient().set(keyInfoId, JsonUtil.toJson(store), CACHED_STORE_DB_INDEX);
		}
		*/
		return returnno;
	}


}
